{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import backtrader as bt\n",
    "import datetime\n",
    "\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-04</th>\n",
       "      <td>2.554</td>\n",
       "      <td>2.560</td>\n",
       "      <td>2.499</td>\n",
       "      <td>2.530</td>\n",
       "      <td>536470795.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-07</th>\n",
       "      <td>2.522</td>\n",
       "      <td>2.544</td>\n",
       "      <td>2.516</td>\n",
       "      <td>2.534</td>\n",
       "      <td>414827961.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-08</th>\n",
       "      <td>2.533</td>\n",
       "      <td>2.539</td>\n",
       "      <td>2.503</td>\n",
       "      <td>2.523</td>\n",
       "      <td>400097236.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-09</th>\n",
       "      <td>2.519</td>\n",
       "      <td>2.534</td>\n",
       "      <td>2.504</td>\n",
       "      <td>2.521</td>\n",
       "      <td>439988330.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-10</th>\n",
       "      <td>2.522</td>\n",
       "      <td>2.553</td>\n",
       "      <td>2.510</td>\n",
       "      <td>2.527</td>\n",
       "      <td>455625049.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-25</th>\n",
       "      <td>3.985</td>\n",
       "      <td>3.997</td>\n",
       "      <td>3.973</td>\n",
       "      <td>3.992</td>\n",
       "      <td>310578426.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-26</th>\n",
       "      <td>3.989</td>\n",
       "      <td>4.026</td>\n",
       "      <td>3.987</td>\n",
       "      <td>4.021</td>\n",
       "      <td>332255940.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-27</th>\n",
       "      <td>4.025</td>\n",
       "      <td>4.068</td>\n",
       "      <td>4.016</td>\n",
       "      <td>4.017</td>\n",
       "      <td>436813014.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-30</th>\n",
       "      <td>4.006</td>\n",
       "      <td>4.088</td>\n",
       "      <td>4.000</td>\n",
       "      <td>4.075</td>\n",
       "      <td>542381449.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>4.069</td>\n",
       "      <td>4.099</td>\n",
       "      <td>4.062</td>\n",
       "      <td>4.096</td>\n",
       "      <td>352185905.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1703 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             open   high    low  close       volume\n",
       "date                                               \n",
       "2013-01-04  2.554  2.560  2.499  2.530  536470795.0\n",
       "2013-01-07  2.522  2.544  2.516  2.534  414827961.0\n",
       "2013-01-08  2.533  2.539  2.503  2.523  400097236.0\n",
       "2013-01-09  2.519  2.534  2.504  2.521  439988330.0\n",
       "2013-01-10  2.522  2.553  2.510  2.527  455625049.0\n",
       "...           ...    ...    ...    ...          ...\n",
       "2019-12-25  3.985  3.997  3.973  3.992  310578426.0\n",
       "2019-12-26  3.989  4.026  3.987  4.021  332255940.0\n",
       "2019-12-27  4.025  4.068  4.016  4.017  436813014.0\n",
       "2019-12-30  4.006  4.088  4.000  4.075  542381449.0\n",
       "2019-12-31  4.069  4.099  4.062  4.096  352185905.0\n",
       "\n",
       "[1703 rows x 5 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df300 = pd.DataFrame()\n",
    "\n",
    "for i in range(7):\n",
    "    df = pd.read_csv('../hist_data/510300_D_{}.csv'.format(2013+i), parse_dates=True, index_col=0)\n",
    "    df300 = pd.concat([df300,df])\n",
    "    \n",
    "df300"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a Stratey\n",
    "class PatternStrategy(bt.Strategy):\n",
    "    \n",
    "    def log(self, txt):\n",
    "        ''' Logging function for this strategy'''\n",
    "        dt = self.datas[0].datetime.date(0)\n",
    "        print('%s, %s' % (dt.isoformat(), txt))\n",
    "        \n",
    "    def __init__(self):\n",
    "        self.pa1 = bt.talib.CDL3LINESTRIKE(\n",
    "            self.datas[0].open,self.datas[0].high,self.datas[0].low,self.datas[0].close)\n",
    "        \n",
    "        self.pa2 = bt.talib.CDLXSIDEGAP3METHODS(\n",
    "            self.datas[0].open,self.datas[0].high,self.datas[0].low,self.datas[0].close)\n",
    "\n",
    "    def next(self):\n",
    "        pav = self.pa1[0]\n",
    "        if pav!=0.0:\n",
    "            self.log('pa1, {:.2f}'.format(pav))\n",
    "            \n",
    "        pav = self.pa2[0]\n",
    "        if pav!=0.0:\n",
    "            self.log('pa2, {:.2f}'.format(pav))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2013-01-16, pa2, 100.00\n",
      "2013-02-07, pa2, 100.00\n",
      "2014-06-17, pa2, 100.00\n",
      "2015-02-25, pa2, 100.00\n",
      "2015-06-15, pa1, 100.00\n",
      "2015-09-16, pa2, -100.00\n",
      "2016-01-29, pa2, -100.00\n",
      "2016-09-19, pa2, -100.00\n",
      "2016-11-01, pa1, -100.00\n",
      "2017-07-17, pa2, 100.00\n",
      "2018-05-31, pa2, -100.00\n",
      "2018-06-13, pa2, 100.00\n",
      "2018-06-29, pa2, -100.00\n",
      "2018-07-16, pa2, 100.00\n",
      "2019-03-15, pa2, -100.00\n",
      "2019-11-01, pa1, -100.00\n"
     ]
    }
   ],
   "source": [
    "cerebro = bt.Cerebro()\n",
    "\n",
    "cerebro.adddata(bt.feeds.PandasData(dataname=df300, openinterest=None), name= 'etf300')\n",
    "cerebro.addstrategy(PatternStrategy)\n",
    "\n",
    "cerebro.broker.setcash(10000.0)\n",
    "\n",
    "result = cerebro.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "cerebro.plot(iplot=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
